﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TOOLS;
namespace BLL
{
    public  class DataSetting
    {
        public static string GetAllKey(string key)
        {
            var dalKeyValue = new DAL.KeyValue();
            var temp = dalKeyValue.GetAll().OrderByDescending(p => p.key_id).ToList();
            if (temp != null)
            {
                List<string> result = new List<string>();
                foreach (var item in temp)
                {
                    result.Add(item.key_id);
                }
                return result.ToJSON();
            }
            else
            {
                return null;
            }
        }
        public static Model.tb_key_value GetKeyValue(string key)
        {
            var dalKeyValue = new DAL.KeyValue();
            var temp = dalKeyValue.GetOne(p=>key==p.key_id);
            if (temp!=null)
            {
                return temp;
            }
            else
            {
                return null;
            }
        }

        public static bool AddKeyValue(string v)
        {
            var dalKeyValue = new DAL.KeyValue();
            Model.tb_key_value entity = new Model.tb_key_value();
            entity.key_id = v;
            entity.value = v;
            entity.note = v;
            entity.create_time = DateTime.Now;
            return dalKeyValue.Add(entity);
        }
        public static bool UpdateKeyValue(string key, string value, string note)
        {
            var dalKeyValue = new DAL.KeyValue();
            var entity = dalKeyValue.GetOne(p=>key==p.key_id);
            entity.value = value;
            entity.note = note;
            entity.create_time = DateTime.Now;
           return dalKeyValue.Update(entity);
        }
        public static Dictionary<string, string> GetFA()
        {
            var dalKeyValue = new DAL.KeyValue();
           Dictionary<string, string> result = new Dictionary<string, string>();
           var temp= dalKeyValue.GetList(
           p => p.key_id == "f1d"||
           p.key_id == "f1z" ||
           p.key_id == "f1x" ||
           p.key_id == "f2d" ||
           p.key_id == "f2z" ||
           p.key_id == "f2x" ||
           p.key_id == "f3d" ||
           p.key_id == "f3z" ||
           p.key_id == "f3x").ToList();
            if (temp==null)
            {
                return null;
            }
            foreach (var item in temp)
            {
                string[] strArr = item.value.Split(new string[] { "==" }, StringSplitOptions.None);
                #region 扰乱顺序
                var strItem = strArr[0].Split(',').ToList();
                StringBuilder tempResult = new StringBuilder();
                Random d = new Random();
                int i;
                while (strItem.Count!=0)
                {
                    i =  d.Next(0,strItem.Count);
                    tempResult.Append(strItem[i]+",");
                    strItem.RemoveAt(i);
                }
                strArr[0] = tempResult.ToString();
                strArr[0]=strArr[0].Remove(strArr[0].Length - 1);
                #endregion
                result.Add(item.key_id,strArr[0]);
                result.Add(item.key_id+"bs", strArr[1]);
            }
            return result;
        }


        internal static Model.tb_key_value GetChangeTimeConfig(int scriptId)
        {
            var dalKeyValue = new DAL.KeyValue();
            string keyString="ChangeTimeConfig_"+scriptId;
            var entity = dalKeyValue.GetOne(p => p.key_id == keyString);
            return entity;
        }
    }
}
